package com.situ.staffmgr.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.situ.staffmgr.entity.Admin;
import com.situ.staffmgr.service.AdminService;
import com.situ.staffmgr.serviceImpl.AdminServiceImpl;
import com.situ.staffmgr.util.BaseServlet;

@WebServlet("/admin.do")
public class AdminServlet extends BaseServlet{
	private AdminService adminService=new AdminServiceImpl();
	public void login(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
		//获取参数
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		//业务逻辑
		try {
			//登陆成功
			Admin admin=adminService.login(username, password);
			HttpSession session=request.getSession();
			session.setAttribute("admin", admin);
			response.sendRedirect("./");
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//登陆失败
			e.printStackTrace();
			request.setAttribute("error", e.getMessage());
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}
	public void logout(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
		//清空session
		HttpSession session=request.getSession();
		session.invalidate();
		//跳转页面
		response.sendRedirect("login.jsp");
		
	}
}
